/* eslint-disable no-unused-vars */
import _findIndex from "lodash/findIndex";
import state from "../libs/status";

const digitsRE = /(\d{3})(?=\d)/g;
import "@/libs/proto";

export function currency(value, currency, decimals) {
  value = parseFloat(value);
  if (!isFinite(value) || (!value && value !== 0)) return "";
  currency = currency != null ? currency : "$";
  decimals = decimals != null ? decimals : 2;
  var stringified = Math.abs(value).toFixed(decimals);
  var _int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  var i = _int.length % 3;
  var head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? "," : "") : "";
  var _float = decimals ? stringified.slice(-1 - decimals) : "";
  var sign = value < 0 ? "-" : "";
  return (
    sign + currency + head + _int.slice(i).replace(digitsRE, "$1,") + _float
  );
}

export function formatTime(date, format) {
  if (null === date || undefined === date) {
    return "";
  }
  return new Date(date).Format(format);
}

export function keyText(key, stateKey) {
  if (key || key !== undefined || key !== "") {
    const index = _findIndex(getDictionary(stateKey), o => o.keyword === key);
    if (index >= 0) {
      return getDictionary(stateKey)[index].label;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

export function keyTextById(id, stateKey) {
  if (id || id !== undefined || id !== "") {
    const index = _findIndex(getDictionary(stateKey), o => o.id === Number(id));
    if (index >= 0) {
      return getDictionary(stateKey)[index].label;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

export function keyTextStatus(key, stateKey) {
  if (key || key !== undefined || key !== "") {
    const index = _findIndex(state[stateKey], o => o.value === key);
    if (index >= 0) {
      return state[stateKey][index].text;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

export function getDictionary(code) {
  if (!code) return;
  let DictionaryData = window.sessionStorage.getItem("allDic");
  let arr = JSON.parse(DictionaryData);
  let obj = arr.find(v => v.keyword === code);
  if (obj) {
    return obj.children;
  }
  // else {
  //   obj = getChildDictionary(code,arr)
  //   if (obj){
  //     return obj
  //   }
  // }
  return [];
}

// // 根据值集pCode获取子集值集选项列表
// export function getChildDictionary(code, child) {
//   if (!child) return
//   if (!code) return
//   for(let i=0;i<child.length;i++){
//     let item = child[i]
//     console.log(item)
//     if (item) {
//       if (item.value === code) {
//         return item.children
//       } else {
//         if (item.children){
//           console.log(item.children,5555)
//          return  getChildDictionary(code,item.children)
//         }
//       }
//     }
//   }
// }

export function dataTime(value) {
  if (value !== "" && value !== undefined && value !== null) {
    let date = new Date(value); //把定义的时间赋值进来进行下面的转换
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    return year + "-" + month + "-" + day;
  }
}

export function getWaitDays(value) {
  if (value !== "" && value !== undefined && value !== null) {
    let date = new Date(formatTime(new Date(value), "yyyy-MM-dd 00:00:00")); //把定义的时间赋值进来进行下面的转换
    let nowData = new Date(formatTime(new Date(), "yyyy-MM-dd 00:00:00"));
    let dateSpan = nowData - date;
    dateSpan = Math.abs(dateSpan);
    let iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays + "天";
  }
}

export function unitTrans(value) {
  value = parseFloat(value);
  if (!isFinite(value) || (!value && value !== 0)) return "";
  if (value >= 10000) {
    return value / 10000 + "亿元";
  } else {
    return value + "万元";
  }
}

export function more(value) {
  return value.replace(/更多/, "");
}

export function trim(value) {
  return value.trim();
}

export function fullImagePath(uri) {
  if (uri) {
    if (
      uri.indexOf("http") === 0 ||
      uri.indexOf("../") === 0 ||
      uri.indexOf("data:") === 0
    ) {
      return uri;
    } else {
      return window.api.imageDomain + uri;
    }
  } else {
    return "";
  }
}

//年龄
export function ageFilter(value) {
  if (!value) return "";
  let birthdays = new Date(value.replace(/-/g, "/"));
  let d = new Date();
  let age =
    d.getFullYear() -
    birthdays.getFullYear() -
    (d.getMonth() < birthdays.getMonth() ||
    (d.getMonth() == birthdays.getMonth() && d.getDate() < birthdays.getDate())
      ? 1
      : 0);
  return age + "岁";
}

// export function tempType(value) {
//   if (value =="DIR") {
//     return "文件夹";
//   } else {
//     return "文件";
//   }
// }
